package com.css.dzj.zfjg.modules.feedback.controller;

import com.alibaba.fastjson.JSON;
import com.css.dzj.zfjg.common.audit.service.SysAuditService;
import com.css.dzj.zfjg.common.util.BeanUtil;
import com.css.dzj.zfjg.common.util.Constant;
import com.css.dzj.zfjg.common.util.R;
import com.css.dzj.zfjg.modules.feedback.entity.JgLhjgDffkEntity;
import com.css.dzj.zfjg.modules.feedback.service.JgLhjgDffkService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 联合监管答复完善Controller
 */
@RestController
@RequestMapping("/feedback")
public class JgLhjgDffkController {

    @Autowired
    private JgLhjgDffkService jgLhjgDffkService;

    // base日志接口
    @Autowired
    SysAuditService sysAuditService;
    @Autowired
    HttpServletRequest request;

    /**
     * 添加联合监管答复完善
     * @param jgLhjgDffkEntity
     * @return
     */
    @PostMapping("/add")
    public R addJgLhjgDffkEntity(@RequestBody JgLhjgDffkEntity jgLhjgDffkEntity, HttpServletRequest request) {
        String s ="";
        try {
            if(StringUtils.isEmpty(jgLhjgDffkEntity)) {
                return R.error("信用线索企业详情不能为空");
            }
            if(StringUtils.isEmpty(jgLhjgDffkEntity.getUnionDeptname()) || StringUtils.isEmpty(jgLhjgDffkEntity.getSuperviseItemCode()) || StringUtils.isEmpty(jgLhjgDffkEntity.getFkIdea()) || StringUtils.isEmpty(jgLhjgDffkEntity.getFkInfo()) ||
                    StringUtils.isEmpty(jgLhjgDffkEntity.getFkUnionType())) {
                return R.error("必填字段不能为空");
            }
            jgLhjgDffkService.add(jgLhjgDffkEntity, request);
            s = JSON.toJSONString(jgLhjgDffkEntity);
            sysAuditService.saveSuccessAudit("联合监管答复信息添加成功", Constant.ZFJG_MODULE_FEEDBACK, Constant.PT, Constant.ZFJG_ADD, s, request);
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("联合监管答复信息添加失败", Constant.ZFJG_MODULE_FEEDBACK, Constant.YZ, Constant.ZFJG_ADD, s, e, request);
            return R.error(e.getMessage());
        }

    }


    /**
     * 更新企业线索信用详情
     * @param jgLhjgDffkEntity
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody JgLhjgDffkEntity jgLhjgDffkEntity) {
        String s = "";
        try {
            if(StringUtils.isEmpty(jgLhjgDffkEntity)) {
                return R.error("监管答复信息不能为空!");
            }
            JgLhjgDffkEntity old = jgLhjgDffkService.findById(jgLhjgDffkEntity.getUuid());
            BeanUtil.copyBeanNotEmptyToBean(jgLhjgDffkEntity, old);
            jgLhjgDffkEntity.setUpdateTime(new Date());
            jgLhjgDffkService.update(jgLhjgDffkEntity);
            s = JSON.toJSONString(jgLhjgDffkEntity);
            sysAuditService.saveSuccessAudit("联合监管答复信息修改成功", Constant.ZFJG_MODULE_FEEDBACK, Constant.PT, Constant.ZFJG_UPDATE, s, request);
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("联合监管答复信息修改失败", Constant.ZFJG_MODULE_FEEDBACK, Constant.YZ, Constant.ZFJG_UPDATE, s, e, request);
            return R.error(e.getMessage());
        }
    }
    /**
     * 通过id查询信用线索企业详情
     * @param uuid
     * @return
     */
    @GetMapping("/queryById")
    public R queryById(@RequestParam String uuid) {
        String s = "";
        try {
            JgLhjgDffkEntity jgLhjgDffkEntity = jgLhjgDffkService.findById(uuid);
            s = JSON.toJSONString(jgLhjgDffkEntity);
            sysAuditService.saveSuccessAudit("通过id获取联合监管答复信息成功", Constant.ZFJG_MODULE_FEEDBACK, Constant.PT, Constant.ZFJG_SELECT, s, request);
            return R.ok().put("data", jgLhjgDffkEntity);
        } catch(Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("通过id获取联合监管答复信息失败", Constant.ZFJG_MODULE_FEEDBACK, Constant.YZ, Constant.ZFJG_SELECT, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 查询联合监管的答复信息
     * @param pageSize
     * @param pageNo
     * @return
     */
    @GetMapping("/queryAll")
    public R queryAll(@RequestParam Integer pageSize, @RequestParam Integer pageNo) {
        String s = "";
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<JgLhjgDffkEntity> jgLhjgDffkEntityQueryResults = jgLhjgDffkService.queryAll(pageRequest);
            s =JSON.toJSONString(jgLhjgDffkEntityQueryResults);
            sysAuditService.saveSuccessAudit("分页查询联合监管答复信息成功", Constant.ZFJG_MODULE_FEEDBACK, Constant.PT, Constant.ZFJG_PAGE, s, request);
            return R.ok().put("data", jgLhjgDffkEntityQueryResults);
        } catch(Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("分页查询联合监管答复信息失败", Constant.ZFJG_MODULE_FEEDBACK, Constant.YZ, Constant.ZFJG_PAGE, s, e, request);
            return R.error(e.getMessage());
        }
    }


    /**
     * 多条件分页查询联合监管答复信息
     * @param pageSize
     * @param pageNo
     * @param unionDeptname
     * @param fkIdea
     * @param fkUnionType
     * @param superviseItemCode
     * @return
     */
    @GetMapping("listJgLhjgDffkEntity")
    public R listClue(@RequestParam Integer pageSize, @RequestParam Integer pageNo,
                      @RequestParam String unionDeptname, @RequestParam String fkIdea,
                      @RequestParam String fkUnionType, @RequestParam String superviseItemCode) {
        String s = "";
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<JgLhjgDffkEntity> jgLhjgDffkEntityQueryResults = jgLhjgDffkService.listJgLhjgDffkEntity(pageRequest, unionDeptname, fkIdea, fkUnionType, superviseItemCode);
            s = JSON.toJSONString(jgLhjgDffkEntityQueryResults);
            sysAuditService.saveSuccessAudit("多条件分页查询联合监管答复信息成功", Constant.ZFJG_MODULE_FEEDBACK, Constant.PT, Constant.ZFJG_PAGE, s, request);
            return R.ok().put("data", jgLhjgDffkEntityQueryResults);
        } catch(Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("多条件分页查询联合监管答复信息失败", Constant.ZFJG_MODULE_FEEDBACK, Constant.YZ, Constant.ZFJG_PAGE, s, e, request);
            return R.error(e.getMessage());
        }
    }
    @GetMapping("/delete")
    public R delete(@RequestParam String uuid) {
        String s = "";
        try {
            if(StringUtils.isEmpty(uuid)) {
                return R.error("监管答复id不能为空");
            }
            JgLhjgDffkEntity jgLhjgDffkEntity = jgLhjgDffkService.findById(uuid);
            if(StringUtils.isEmpty(jgLhjgDffkEntity)) {
                return R.error("监管答复不能为空");
            }
            jgLhjgDffkService.delete(jgLhjgDffkEntity);
            s = JSON.toJSONString(jgLhjgDffkEntity);
            sysAuditService.saveSuccessAudit("联合监管答复信息删除成功", Constant.ZFJG_MODULE_FEEDBACK, Constant.PT, Constant.ZFJG_DELETE, s, request);
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("联合监管答复信息删除失败", Constant.ZFJG_MODULE_FEEDBACK, Constant.YZ, Constant.ZFJG_DELETE, s, e, request);
            return R.error(e.getMessage());
        }
    }


}
